package com.campus.service.impl;

import com.campus.dao.UserDao;
import com.campus.model.Permission;
import com.campus.model.Role;
import com.campus.model.User;
import com.campus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author HLQ
 * @date 2024/11/19 16:21
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public User login(String username, String password){
        User user = userDao.selectOne(username,password);
        Assert.notNull(user);
        return user;
    }
    @Override
    public int addUser(User user){
        return userDao.addUser(user);
    }
    @Override
    public User queryUserById(int id){
        return userDao.queryUserById(id);
    }
    @Override
    public User queryUserByName(String name){
        return userDao.queryUserByName(name);
    }
    @Override
    public int addUserRole(int userId,int roleId){
        String sql = "INSERT INTO t_sys_user_role (user_id,role_id) VALUES (?,?)";
        return jdbcTemplate.update(sql,userId,roleId);
    }
    @Override
    public int addRole(Role role){
        String sql = "INSERT INTO t_sys_role (name,description) VALUES (?,?)";
        Object[] args = new Object[]{role.getName(),role.getDescription()};
        return jdbcTemplate.update(sql,args);
    }
    @Override
    public int addPermission(Permission permission){
        String sql = "INSERT INTO t_sys_permission (name,code,url,`order`) VALUES (?,?,?,?)";
        Object[] args = new Object[]{
                permission.getName(),
                permission.getCode(),
                permission.getUrl(),
                permission.getOrder()
        };
        return jdbcTemplate.update(sql,args);
    }
    @Override
    public int addGroup(String groupName,int classId){
        String sql = "INSERT INTO t_manager_group (group_name,class_id) VALUES (?,?)";
        return jdbcTemplate.update(sql,groupName,classId);
    }

}
